/*********************************************************************
FILE : frmMonthlyReportView.cs
DESCRIPTION: Form frmMonthlyReportView dung de hien bao cao.
VERSION:
PROGRAMMER : Pham Minh Tuan		 
 *********************************************************************/
using System; 
using System.Drawing;
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data.SqlClient;
using System.Data; 
using CrystalDecisions.Shared; 
using CrystalDecisions.CrystalReports.Engine; 
using PointOfSale.Global;

namespace PointOfSale
{ 
	/// <summary> 
	/// Summary description for frmMonthlyReportView. 
	/// </summary> 
	public class frmMonthlyReportView : System.Windows.Forms.Form 
	{ 	
		public int intMonth;
		public string strMonth;
		public int intYear;
		public string strDepartmentName;

		string report_file = ""; 
		ReportDocument crReportDocument = new ReportDocument();
		private CrystalDecisions.Windows.Forms.CrystalReportViewer crpViewer; 
		/// <summary> 
		/// Required designer variable. 
		/// </summary> 
		private System.ComponentModel.Container components = null; 

		public frmMonthlyReportView() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 

			// 
			// TODO: Add any constructor code after InitializeComponent call 
			// 
		} 

		/// <summary> 
		/// Clean up any resources being used. 
		/// </summary> 
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 

#region Windows Form Designer generated code 
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		/// </summary> 
		private void InitializeComponent() 
		{ 
			this.crpViewer = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.SuspendLayout();
			// 
			// crpViewer
			// 
			this.crpViewer.ActiveViewIndex = -1;
			this.crpViewer.DisplayGroupTree = false;
			this.crpViewer.Dock = System.Windows.Forms.DockStyle.Fill;
			this.crpViewer.Location = new System.Drawing.Point(0, 0);
			this.crpViewer.Name = "crpViewer";
			this.crpViewer.ReportSource = null;
			this.crpViewer.ShowGroupTreeButton = false;
			this.crpViewer.Size = new System.Drawing.Size(712, 381);
			this.crpViewer.TabIndex = 0;
			// 
			// frmMonthlyReportView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(712, 381);
			this.Controls.Add(this.crpViewer);
			this.Name = "frmMonthlyReportView";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Report View";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmMonthlyReportView_Load);
			this.ResumeLayout(false);

		} 
#endregion 

		private DataColumn CreateNewColumn( string sColumnType, string sColumnCaption,string sColumnName)
		{
			DataColumn dtColumn = new DataColumn();			
			dtColumn.DataType = System.Type.GetType("System." + sColumnType);
			dtColumn.ColumnName = sColumnName;
			dtColumn.Caption = sColumnCaption;			
			dtColumn.AutoIncrement = false;			
			dtColumn.ReadOnly = false;
			dtColumn.Unique = false;
			return dtColumn;
		}

		private void frmMonthlyReportView_Load(object sender, System.EventArgs e)
		{

			//load file report
			
			Report.Monthly.rptMonthlyReport crReportDocument = new Report.Monthly.rptMonthlyReport();
			
			// Su dung store procedure
			SqlCommand cmd = new SqlCommand("spMonthlySummaryReport",clsGlobal.g_DataConn);
			cmd.CommandType = CommandType.StoredProcedure;

			//tao cac tham so cho store procedure
			SqlParameter myParmMonth = cmd.Parameters.Add("@Month", SqlDbType.Int);
			SqlParameter myParmYear = cmd.Parameters.Add("@Year", SqlDbType.Int);
			SqlParameter myParmDateNumber = cmd.Parameters.Add("@DateNumber", SqlDbType.Int);
			SqlParameter myParmDepartmentName = cmd.Parameters.Add("@DepartmentName", SqlDbType.NVarChar);

			SqlParameter myParmTotal_Amount = cmd.Parameters.Add("@Total_Amount", SqlDbType.Float);
			SqlParameter myParmTotal_Tax = cmd.Parameters.Add("@Total_Tax", SqlDbType.Float);
			SqlParameter myParmTotal_DisAmount = cmd.Parameters.Add("@Total_DisAmount", SqlDbType.Float);
			SqlParameter myParmTotal_PaidAmount = cmd.Parameters.Add("@Total_PaidAmount", SqlDbType.Float);
			SqlParameter myParmTotal_PostAmount = cmd.Parameters.Add("@Total_PostAmount", SqlDbType.Float);
			SqlParameter myParmTotal_Balance = cmd.Parameters.Add("@Total_Balance", SqlDbType.Float);

			//Thiet lap gia tri va huong cho cac tham so cua store procedure
			//Gia tri cua cac tham so INPUT
			myParmMonth.Value = intMonth;
			myParmYear.Value = intYear;
			myParmDateNumber.Value = System.DateTime.DaysInMonth(intYear,intMonth);
			myParmDepartmentName.Value = strDepartmentName;
			//Huong cua cac tham so
			myParmMonth.Direction = ParameterDirection.Input;
			myParmYear.Direction = ParameterDirection.Input;
			myParmDateNumber.Direction = ParameterDirection.Input;
			myParmDepartmentName.Direction =  ParameterDirection.Input;

			myParmTotal_Amount.Direction = ParameterDirection.Output; 
			myParmTotal_Tax.Direction = ParameterDirection.Output;
			myParmTotal_DisAmount.Direction = ParameterDirection.Output;
			myParmTotal_PaidAmount.Direction = ParameterDirection.Output;
			myParmTotal_PostAmount.Direction = ParameterDirection.Output;
			myParmTotal_Balance.Direction = ParameterDirection.Output;

			//Thuc hien store procedure
			SqlDataReader myReader = cmd.ExecuteReader();
				
			//Tao DataTable de chua du lieu lay tu store procedure
			DataTable dtMonthlyReport = new DataTable("spMonthlySummaryReport");
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Int16","","DateNo"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","Amount"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","Tax"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","DisAmount"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","PaidAmount"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","PostAmount"));
			dtMonthlyReport.Columns.Add(CreateNewColumn( "Double","","Balance"));

			//Dua du lieu vao DataTable
			while (myReader.Read())
			{			
				DataRow newRow=dtMonthlyReport.NewRow();
				newRow["DateNo"]= myReader["DateNo"];
				newRow["Amount"]= myReader["Amount"];
				newRow["Tax"]= myReader["Tax"];
				newRow["DisAmount"]= myReader["DisAmount"];
				newRow["PaidAmount"]= myReader["PaidAmount"];
				newRow["PostAmount"]= myReader["PostAmount"];
				newRow["Balance"]= myReader["Balance"];
				dtMonthlyReport.Rows.Add(newRow);
			}
			myReader.Close();	

			//Dua du lieu tu DataTable vao DataSet
			DataSet dsMonthlyReport = new DataSet("spMonthlySummaryReport");
			dsMonthlyReport.Tables.Add(dtMonthlyReport);
			
			//Lay du lieu tu DataSet dua vao Report
			crReportDocument.SetDataSource(dsMonthlyReport);
			
			//Dua cac tham so vao Report
			crReportDocument.SetParameterValue("@CompanyName",clsGlobal.g_CompanyName);
			crReportDocument.SetParameterValue("@CompanyAddress",clsGlobal.g_CompanyAddress);
			crReportDocument.SetParameterValue("@CompanyInfo","Tel: " + clsGlobal.g_CompanyTel + "; "
															  + "Fax: " + clsGlobal.g_CompanyFax + "; "
															  + "Email: " + clsGlobal.g_CompanyEmail);
			crReportDocument.SetParameterValue("@CompanyWebsite","Website: " + clsGlobal.g_CompanyWebsite );
			if (this.strDepartmentName == "(All)")
			{
				crReportDocument.SetParameterValue("@Header","Monthly Summary Report (All Departments) For " 
														+ strMonth + ", " + System.Convert.ToString(intYear));
			}
			else
			{
				crReportDocument.SetParameterValue("@Header","Monthly Summary Report (" 
														+ this.strDepartmentName + ") For " + strMonth
														+ ", " + System.Convert.ToString(intYear));
			}
			crReportDocument.SetParameterValue("@Total_Amount",myParmTotal_Amount.Value );
			crReportDocument.SetParameterValue("@Total_Tax",myParmTotal_Tax.Value );
			crReportDocument.SetParameterValue("@Total_DisAmount",myParmTotal_DisAmount.Value );
			crReportDocument.SetParameterValue("@Total_PaidAmount",myParmTotal_PaidAmount.Value );
			crReportDocument.SetParameterValue("@Total_PostAmount",myParmTotal_PostAmount.Value );
			crReportDocument.SetParameterValue("@Total_Balance",myParmTotal_Balance.Value );

			//Hien Report
			crpViewer.ReportSource = crReportDocument; 			
		}
	} 
} 